// "use strict";
// void 0 === window.util && (window.util =  Object.create(null));
// // const variableSponsor =  window.origin + '/eip-web-sponsor'
// const url = (new URL(window.location.href)).searchParams
// window.apiHost = window.origin + '/'

const util = Object.create(null)
const apiHost = 'http://192.168.18.18:8088/';
// util
// require: axios vant
Object.assign(util,{
  STORE: {
    getItem(k){
      return '';// localStorage.getItem(k)
    },
    setItem(k,v){
      // localStorage.setItem(k,v)
    }
  },
  // STORE: window.localStorage,
  BANTYPE: Object.freeze([".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess",".ini"]),
  // vant: new Vue({ data: {
  // } }),
  alert(msg = '', icon = 'err', throws = false, cb =() => {}) { // 1 suc/2 err
    if (msg) {
      if(!icon){
        vant.Toast(msg);
      }else{
        var ops
        if(typeof icon === 'object'){
          ops = icon
        }else{
          ops = { icon }
        }
        if(ops.icon == 'err'){
          vant.Toast.fail(msg)
        } else if(ops.icon == 'suc') {
          vant.Toast.success(msg)
        } else if(ops.icon == 'loading') {
          vant.Toast.loading(msg)
        } else{
          ops.message = msg
          vant.Toast(ops);
        }
      }
    }
    if (throws) throw new Error('error: ' + msg);
  },
  checkLogin(check=true){
    const orgNum = this.getQs('orgNum', true, '')
    if(check && !this.getLocal('usrname')){
      window.location.href = `login.html?orgNum=${orgNum}`
    }
    return {
      username: this.getLocal('usrname'),
      cid: this.getLocal('cid'),
      zzyUserId: this.getLocal('zzyUserId'),
      orgNum,
    }
  },
  checkReturn(obj, msg = '', icon = '', throws = false) {
    icon = icon || 'err';
    if (obj && obj.state === 1) {
      return true;
    } else {
      if(obj.msg && ['未登录','请登陆','请先登陆','需要登陆'].includes(obj.msg.trim())){
        const orgNum = this.getQs('orgNum', true, '')
        window.location.href = `login.html?orgNum=${orgNum}`
      }
      this.alert(msg || obj.msg || obj.message || '操作失败', icon, throws);
      return false;
    }
  },
  get(url, data, ops){
    return axios.get(apiHost + url, this.JSON2FormData(data))
  },
  getImgUrl(src = '', prefix='/image/'){
    if(!src) return ''
    if(src.startsWith('http') || src.startsWith('//')){
      return src;
    }else{
      // test only
      // return window.origin + prefix + '3eaed16d70e8445b9cabb13cf2d0e3c5.jpg'
      return window.origin + prefix + src
    }
  },
  getHiddenString(value='',start=0,len=0) {
    if (!value) return ''
    const vlen = value.length
    if (vlen<0||start<0||len<0) return '';
    var hidden = '';
    for (var i = 0; i < len; i++) { hidden += '*'; }
    if(vlen <= start ){
      return hidden
    } else if(vlen <= start + len){
      return value.substring(0, start) + hidden
    } else {
      return value.substring(0, start) + hidden + value.substring(start+len);
    }
  },
  getPassLevel(pass) {
    if(typeof pass !== 'string' || !pass) return 0;
    var level = 0;
    // 小写字母，级别+1
    if (/[a-z]/.test(pass)) level++;
    // 数字+1
    if (/[0-9]/.test(pass)) level++;
    // 其他+1
    if (/[^a-z0-9]/.test(pass)) level++;
    return level;
  },
  getLocal(k) {
    if(!k) return '';
    return JSON.parse(this.STORE.getItem('vip-'+k))
  },
  uploadOne(file) {
    const s   = file.name || ''
    const ext = s.substr(s.lastIndexOf('.')+1).toLocaleLowerCase()
    if(this.BANTYPE.includes(ext)) this.alert('非法上传', 'err', true);
    return this.post('eip-web-sponsor/upload/uploadImg',{
        file
      },
      {
        'Content-Type': 'multipart/form-data'
      }
    )
  },
  getQs(k, ifNullGetLocal = false, df=''){
    return ''
    // const url = (new URL(location.href)).searchParams
    // let rs = url.get(k) || '';
    // return ifNullGetLocal ? (rs || this.getLocal(k) || df) : rs;
  },
  JSON2FormData (obj) {
    if (typeof obj !== 'object') return;
    const formData = new FormData();
    Object.keys(obj).forEach(key => formData.append(key, obj[key]));
    return formData;
  },
  postJson(url,data,ops) {
    let df = {
      headers: {
        'Content-Type': 'application/json;charset=utf-8',
      },
      changeOrigin: true,
    }
    return axios.post(window.apiHost + url, data ? JSON.stringify(data) : null ,ops ? Object.assign(df,ops) : df)
  },
  post(url,data,ops) {
    let df = {
      // headers: {
      //  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      //   // 'Content-Type': 'multipart/form-data'
      //   // 'Content-Type': 'application/json;charset=utf-8',
      //   // 'Authorization': window.sessionStorage.getItem("token")
      // },
      changeOrigin: true,
    }
    return axios.post(window.apiHost + url, data ? this.JSON2FormData(data) : null ,ops ? Object.assign(df,ops) : df)
  },
  setObjVal(obj,k,v=''){
    // 未设置的属性 不支持动态绑定
    k.split(',').forEach(i =>{
      obj[i.trim()] = v
    })
  },
  setLocal(k,v) {
    if(!k) return
    this.STORE.setItem('vip-'+k,JSON.stringify(v))
  },
  toast (msg,duration){
    vant.Toast(msg)
    // duration=isNaN(duration)?3000:duration;
    // var m = document.createElement('div');
    // m.innerHTML = msg;
    // m.style.cssText="max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 9999999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
    // document.body.appendChild(m);
    // setTimeout(function() {
    //   var d = 0.5;
    //   m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
    //   m.style.opacity = '0';
    //   setTimeout(function() { document.body.removeChild(m) }, d * 1000);
    // }, duration);
  },
})

export default util